package com.ctg.auth.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ctg.auth.bean.Permisssion;
import com.ctg.auth.service.UserService;
import com.ctg.auth.utils.Token;

@Controller
public class LoginController {
	@Resource
	private UserService userService;
	/**
	 * 用户进入登陆页面
	 */
	@RequestMapping(value="/login")
	public String login() throws IOException {
		return "login";
	}

	/**
	 * 用户输入账号密码，点击登陆
	 * @param userName
	 * @param userPassword
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/gologin")
	@ResponseBody
	public Map<String,Object>  toLogin(String userName,String userPassword,ModelMap model,HttpSession session) {
		String token = Token.genetateToken();
		session.setAttribute("SESSION_TOKEN", token);
		 
		int uid = userService.getUid(userName,userPassword);//获取用户id
		List<Permisssion> permission =  userService.getUserAuth(uid);//获取用户操作权限
		if(permission != null){
		    session.setAttribute("SESSION_PERMISSION",permission);
		}
		 boolean login = false;
		 if(uid>0) {
			 login=true;
		 }
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("login", true);
		map.put("token", token);
		return map;
	}
	
	/**
	 * 登陆成功，跳转到index页面
	 * @return
	 */
	@RequestMapping(value="/goindex")
	public String goIndex() {
		return "index";
	}
}
